package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.CustomException;
import com.yuesystem.dto.responsebody.CommentResponseBody;
import com.yuesystem.entity.Comment;
import com.yuesystem.entity.Reply;
import com.yuesystem.entity.User;
import com.yuesystem.mapper.CommentMapper;
import com.yuesystem.service.CommentService;
import com.yuesystem.service.ReplyService;
import com.yuesystem.service.UserService;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 评论接口实现类.
 */
@Slf4j
@Service
public class CommentServiceImpl extends
    ServiceImpl<CommentMapper, Comment> implements CommentService {

  @Autowired
  private ReplyService replyService;

  @Autowired
  private UserService userService;

  /**
   * 评论帖子.
   *
   * @param comment 评论类
   */
  @Override
  public void saveComment(Comment comment) {
    // 获取评论者id
    Long userId = BaseContext.getCurrentId();
    comment.setUserId(userId);
    if (comment.getContent().isEmpty()) {
      throw new CustomException("评论内容不能为空");
    }
    this.save(comment);

  }


  /**
   * 根据传入的帖子id, 返回该帖子的总评论数量.
   *
   * @param blogId 帖子id
   * @return 帖子的总评论数量
   */
  @Override
  public Long getCommentCount(Long blogId) {
    // 统计二级评论的数量
    LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Reply::getBlogId, blogId);
    Long countSecond = replyService.count(queryWrapper);

    // 统计一级评论的数量
    LambdaQueryWrapper<Comment> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.eq(Comment::getBlogId, blogId);
    Long countFirst = this.count(queryWrapper1);
    Long counts = countFirst + countSecond;

    return counts;
  }


  /**
   * 根据blogId找到相关的所有一级评论，封装到list集合中.
   *
   * @param blogId 帖子id
   * @return 某帖子的所有一级评论
   */
  @Override
  public List<CommentResponseBody> getCommentFist(Long blogId) {
    LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Comment::getBlogId, blogId);
    List<Comment> commentsList = this.list(queryWrapper);

    // 根据blogId找到所有一级评论，包括用户名，封装到了dtoList集合中
    List<CommentResponseBody> commentResponseBodyList = commentsList.stream().map((item) -> {
      CommentResponseBody commentResponseBody = new CommentResponseBody();
      // 拷贝
      BeanUtils.copyProperties(item, commentResponseBody);

      // 获取评论人id
      Long userId = item.getUserId();
      User one = userService.getById(userId);
      // 获取评论人名字
      String userName = one.getUserName();

      if (one != null) {
        commentResponseBody.setUserName(userName);
      }
      return commentResponseBody;
    }).collect(Collectors.toList());

    return commentResponseBodyList;
  }

  /**
   * 根据BLOG的id删除所有和他有关的评论.
   *
   * @param listBlog 帖子id列表
   */
  @Override
  public void deleteByBlog(List<Long> listBlog) {
    LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.in(listBlog.size() != 0, Comment::getBlogId, listBlog);
    this.remove(queryWrapper);
  }


}
